// 类数组转数组
let obj = { 1: 222, 2: 123, 5: 888 };
const result = Array.from({ length: 12 }).map((_, index) => obj[index + 1] || null);
console.log(result) //[222, 123, null, null, 888, null, null, null, null, null, null, null]

// 设计一个lazyamn
class LazyManClass {
  constructor(name) {
    this.taskList = [];
    this.name = name;
    console.log(`Hi I am ${this.name}`);
    setTimeout(() => {
      this.next();
    }, 0);
  }
  eat(food) {
    const fn = () => {
      console.log(`i am eating ${food}`);
      this.next()
    }
    this.taskList.push(fn);
    return this;
  }
  sleepFirst(time) {
    const fn = () => {
      setTimeout(() => {
        console.log(`先等等${time}s`);
        this.next()
      }, time * 1000);
    }
    this.taskList.unshift(fn);
    return this;
  }
  sleep(time) {
    const fn = () => {
      setTimeout(() => {
        console.log(`等等${time}s`);
        this.next()
      }, time * 1000);
    }
    this.taskList.push(fn);
    return this;
  }
  next() {
    var fn = this.taskList.shift();
    fn && fn();
  }
}

function LazyMan(name) {
  return new LazyManClass(name);
}

LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(4).eat('junk food');

// 模拟实现一个 Promise.finally
Promise.prototype.finally = function (cb) {
  return this.then(
    value => Promise.resolve(cb()).then(() => value),
    err => Promise.resolve(cb()).then(() => { throw err })
  )
}

// 位数数组分类
function formArray (arr) {
  const sortedArr = Array.from(new Set(arr)).sort((a, b) => a - b)
  const map = new Map()
  sortedArr.forEach(v => {
    const key = Math.floor(v / 10)
    const group = map.get(key) || []
    group.push(v)
    map.set(key, group)
  })
  return [...map.values()]
}